from bokeh.plotting import figure
from bokeh.palettes import Spectral5
from bokeh.transform import factor_cmap
from bokeh.sampledata.autompg import autompg_clean as df
from bokeh.plotting import output_notebook, figure, show
from pyecharts import options as opts
from pyecharts.charts import Geo,Map


def mpg():
    df.cyl = df.cyl.astype(str)
    # df.yr = df.yr.astype(str)
    group = df.groupby(['cyl', 'mfr'])  # 复合条件分组，[缸数、厂家]
    index_cmap = factor_cmap('cyl_mfr', palette=Spectral5, factors=sorted(df.cyl.unique()), end=1)
    # 画布
    p = figure(plot_width=800, plot_height=300, title="Mean MPG by # Cylinders and Manufacturer",
               x_range=group, tooltips=[("MPG", "@mpg_mean"), ("Cyl, Mfr", "@cyl_mfr")])
    # 绘图
    p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=group,
           line_color="white", fill_color=index_cmap, )  # 尾气排放量均值
    # 其他
    p.y_range.start = 0
    p.x_range.range_padding = 0.05  # 同css中的padding
    p.xgrid.grid_line_color = None
    p.xaxis.axis_label = "Manufacturer grouped by # Cylinders"
    p.xaxis.major_label_orientation = 1.2  # x轴标签旋转
    p.outline_line_color = None

    return p


def vbar_demo():
    p = figure(plot_width=300, plot_height=300)
    p.vbar(
        x=[1, 2, 3, 4],
        width=0.5,
        bottom=0,
        top=[1.7, 2.2, 4.6, 3.9],
        color='navy'
    )
    return p


def pyecharts_map(data):
    ''''pyecharts map绘制'''
    c = (
        Map()
            .add("高校数量", [[p[:-1], sum_values(p)] for p in province_name], "china")
            .set_global_opts(
            title_opts=opts.TitleOpts(title="Map-高校分布"), visualmap_opts=opts.VisualMapOpts(
                max_=150,
                is_piecewise=True
            ),
            legend_opts=opts.LegendOpts(
                pos_left=20,
                pos_top=100
            ),
            toolbox_opts=opts.ToolboxOpts(
                pos_right=20,
                pos_top=20
            )
        )
        #     .render("map_guangdong.html")
    )
    return c
